
//
//  FlowVC.swift
//  Employee
//
//  Created by swordjoy on 2017/6/26.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import Moya

class FlowVC: BaseVC {

    var refreshBefore: VoidClosure?
    
  fileprivate var flows: [Flow] = []
  fileprivate var tableView: UITableView!
  fileprivate var isMores: [Bool] = []
  fileprivate var cancle: Cancellable?
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
  
  
  override func viewDidLoad() {
    super.viewDidLoad()
    create()
    pullData()
  }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        GlobalConfig.baiduStatisticsPageViewStart("选择流程")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        GlobalConfig.baiduStatisticsPageViewEnd("选择流程")
    }
    
  deinit {
    if let temp = cancle { temp.cancel() }
  }
  

}

// MARK: - FlowCellDelegate

extension FlowVC: FlowCellDelegate {
  func flowCellNext(indexPath: IndexPath, location: Int) {
    let vc = FlowWriteVC()
    vc.flowTitle = flows[indexPath.row].flowTypeDtos[location].name
    vc.flowId = flows[indexPath.row].flowTypeDtos[location].id
    vc.refreshBefore = { self.refreshBefore?() }
    navigationController?.pushViewController(vc, animated: true)
  }

  func flowCellMore(indexPath: IndexPath, isMore: Bool) {
    isMores[indexPath.row] = isMore
    tableView.reloadRows(at: [indexPath], with: .fade)
  }
}

// MARK: - UITableViewDataSource

extension FlowVC: UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return flows.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: FlowCell.identifiler) as! FlowCell
    cell.model = flows[indexPath.row]
    cell.indexPath = indexPath
    cell.delegate = self
    cell.isMore = isMores[indexPath.row]
    return cell
  }
}

// MARK: - UITableViewDelegate

extension FlowVC: UITableViewDelegate {
  
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return FlowCell.height(with: isMores[indexPath.row], number: flows[indexPath.row].flowTypeDtos.count)
  }
}

// MARK: - fileprivate

extension FlowVC {
  
  fileprivate func pullData() {
    SJApp.showStateBarHUD()
    cancle = FlowRequest.shared.getFlowType(success: { (datas) in
      self.flows = datas
      for _ in self.flows {
        self.isMores.append(false)
      }
      self.tableView.reloadData()
    }, failure: { 
      SJApp.log("获取流程失败")
    })
  }
  
  fileprivate func create() {
    view.backgroundColor = GlobalConfig.BGWhiteColor
    title = "选择流程"
    addBackItem()
    
    tableView = UITableView()
    tableView.backgroundColor = UIColor.clear
    tableView.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noNavBarHeight)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorStyle = .none
    tableView.register(FlowCell.self, forCellReuseIdentifier: FlowCell.identifiler)
    view.addSubview(tableView)
  }
  
}
